package cn.iocoder.yudao.module.vlog.info.convert;

import cn.iocoder.yudao.framework.business.basic.pojo.vo.UserSimpleVO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.region.RegionUtils;
import cn.iocoder.yudao.framework.region.enums.RegionDataSet;
import cn.iocoder.yudao.module.api.product.spu.dto.ProductSpuRespDTO;
import cn.iocoder.yudao.module.vlog.info.dal.dataobject.VLogInfoDO;
import cn.iocoder.yudao.module.vlog.info.vo.AppVLogInfoCreateReqVO;
import cn.iocoder.yudao.module.vlog.info.vo.VLogInfoRespVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.Collection;
import java.util.Map;

@Mapper
public interface VLogInfoConvert {

    VLogInfoConvert INSTANCE = Mappers.getMapper(VLogInfoConvert.class);

    @Mapping(target = "userId", ignore = true)
    @Mapping(target = "status", ignore = true)
    @Mapping(target = "shopId", ignore = true)
    @Mapping(target = "shareCount", ignore = true)
    @Mapping(target = "likedCount", ignore = true)
    @Mapping(target = "id", ignore = true)
    @Mapping(target = "favoriteCount", ignore = true)
    @Mapping(target = "distance", ignore = true)
    @Mapping(target = "commentCount", ignore = true)
    @Mapping(target = "browseCount", ignore = true)
    @Mapping(target = "width", source = "videoWidth")
    @Mapping(target = "height", source = "videoHeight")
    @Mapping(target = "seconds", source = "videoDuration")
    @Mapping(target = "size", source = "videoSize")
    VLogInfoDO convert(AppVLogInfoCreateReqVO bean);

    @Mapping(target = "provinceName", ignore = true)
    @Mapping(target = "provinceId", ignore = true)
    @Mapping(target = "provinceCode", ignore = true)
    @Mapping(target = "cityName", ignore = true)
    @Mapping(target = "cityCode", ignore = true)
    @Mapping(target = "spuList", ignore = true)
    @Mapping(target = "user", ignore = true)
    @Mapping(target = "shop", ignore = true)
    VLogInfoRespVO convert(VLogInfoDO bean);

    PageResult<VLogInfoRespVO> convertPage(PageResult<VLogInfoDO> page);

    default PageResult<VLogInfoRespVO> convertPage(PageResult<VLogInfoDO> pageResult,
                                                   Map<Long, UserSimpleVO> users,
                                                   Map<Long, Collection<ProductSpuRespDTO>> spus) {
        PageResult<VLogInfoRespVO> page = convertPage(pageResult);
        page.getList().forEach(vo -> {
            vo.setUser(users.get(vo.getUserId()));
            vo.setSpuList(spus.get(vo.getId()));
            RegionUtils.autoAssignCity(RegionDataSet.ONLY_LATEST, vo.getCityCode(), vo);
        });
        return page;
    }

}
